package com.integrate.redis.controller;

import com.integrate.redis.config.RedisLock;
import com.integrate.redis.config.listener.RedisKeyExpirationListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 刘志强
 * @date 2020/11/13 15:28
 */
@RestController
@Slf4j
@CrossOrigin
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisKeyExpirationListener redisKeyExpirationListener;

    @Autowired
    @Qualifier("redisTemplateJdk")
    private RedisTemplate redisTemplateJdk;

    @Autowired
    private RedisLock redisLock;

    @GetMapping("redisSet")
    public String redisSet(String key,String val) {
        redisTemplate.opsForValue().set(key,val);
        Object object = redisTemplate.opsForValue().get(key);
        log.info(object.toString());
        redisTemplate.expire(key,10, TimeUnit.SECONDS);
        redisTemplate.delete(key);

        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");


        redisTemplate.opsForList().rightPushAll("list", list);
        List<String> stringList = redisTemplate.opsForList().range("list", 0, -1);
        stringList.forEach(str -> {
            log.info(str);
        });
        redisTemplate.delete("list");


        redisTemplateJdk.opsForValue().set("listJDK",list);
        object = redisTemplateJdk.opsForValue().get("listJDK");
        list = (List<String>) object;
        list.forEach(str -> {
            log.info(str);
        });
        redisTemplate.delete("listJDK");



        if (redisLock.getLock(key)) {
            // 处理程序
            // ....
            // ......
            // 程序处理结束 删除释放锁
            redisLock.unLock(key);
        } else {
            // 此任务已被别的程序处理
        }

        // map 像map中追加数据 如果没有map 创建map key
        redisTemplate.opsForHash().put("map","key-name","刘志强");
        redisTemplate.opsForHash().put("map","key-age","26");
        Map<String,String> map = new HashMap<>(2);
        map.put("height","178");
        map.put("weight","170");
        // 像map中追加数据
        redisTemplate.opsForHash().putAll("map",map);
        // 取出指定map中的指定字段
        object = redisTemplate.opsForHash().get("map","key-name");
        // 取出整个map
        object = redisTemplate.opsForHash().entries("map");
        // 删除指定map中的指定值
        redisTemplate.opsForHash().delete("map","key-name");
        object = redisTemplate.opsForHash().entries("map");
        redisTemplate.delete("map");
        // 事务开始
        redisTemplate.execute((RedisCallback) redisConnection -> {
            redisConnection.multi();
            if (key.equals("aa")) {
                redisTemplate.opsForValue().set(key,val);
                // 提交事务
                redisConnection.exec();
            } else {
                // 取消事务
                redisConnection.discard();
            }
            return null;
        });


        object = redisTemplate.opsForValue().get(key);
        if (object != null) {
            redisTemplate.delete("aa");
        }

        return "已设置";
    }

    @GetMapping("redisExpireSet")
    public String redisExpireSet(String key,String val,Long millisecond) {
        redisKeyExpirationListener.monitorSet(key,val,millisecond,() -> {
            log.info(key+ "已到期");
        });
        return "已设置";
    }
}
